/**
 * created by 小卷毛, 2023-05-03
 * Copyright (c) 2023, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.ui.home

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isInvisible
import androidx.recyclerview.widget.RecyclerView
import com.rzico.sbl.databinding.ItemManualCallBinding
import com.rzico.sbl.model.RecordData
import com.rzico.sbl.ui.manual.ManualMobileActivity
import com.xinnuo.common.extend.conditionIf
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.extend.orEmpty
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.helper.TimeHelper
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.helper.longValue
import com.xinnuo.common_ui.utils.showBottomPhoneDialog
import java.util.Date
import kotlin.math.abs

/**
 * 描 述：
 * 作 者：小卷毛  2023-05-03 09:16
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class AutoPollAdapter(val list: List<RecordData>) :
    RecyclerView.Adapter<AutoPollAdapter.ViewHolder>() {

    inner class ViewHolder(binding: ItemManualCallBinding) : RecyclerView.ViewHolder(binding.root) {
        val itemTel = binding.itemManualTel
        val itemTime = binding.itemManualTime
        val itemNew = binding.itemManualNew
        val itemFrom = binding.itemManualFrom
        val itemNext = binding.itemManualNext
        val itemContext: Context = binding.root.context
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        ViewHolder(
            ItemManualCallBinding
                .inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
        )

    override fun getItemCount() = conditionIf(list.size < 4, list.size, Int.MAX_VALUE)

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.apply {
            val item = list[position % list.size]

            itemTel.text = item.mobile
            itemTime.text =
                when {
                    abs(item.createDate.longValue - Date().time) < 30 * 60 * 1000L ->
                        TimeHelper.getDiffTime(item.createDate.longValue)

                    else -> item.createDate.getTimeString("MM-dd HH:mm")
                }
            itemNext.text =
                conditionIf(
                    item.status == "confirmed",
                    "再来一单",
                    "去下单"
                )

            itemNew.isInvisible = !item.newer
            itemFrom.isInvisible = item.source != "box"

            itemTel.oneClick {
                itemContext
                    .showBottomPhoneDialog(item.mobile)
            }

            itemNext.oneClick {
                itemContext
                    .startActivityEx<ManualMobileActivity>(
                        "mobile" to item.mobile,
                        "memberId" to item.memberId.orEmpty()
                    )
            }
        }
    }

}